package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.response.StudentScore;
import com.xmy.cultivate.entity.views.EnteringStudent;
import com.xmy.cultivate.entity.views.ScoreData;
import com.xmy.cultivate.entity.views.ScoreDataDetail;
import com.xmy.cultivate.mapper.CourseMapper;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.mapper.TranscriptMapper;
import com.xmy.cultivate.mapper.TranscriptSetMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 成绩管理 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-04-14
 */
@Service
public class TranscriptServiceImpl extends ServiceImpl<TranscriptMapper, Transcript> implements ITranscriptService {


    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    TranscriptMapper transcriptMapper;

    @Autowired
    TranscriptSetMapper transcriptSetMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStudentService iStudentService;

    @Autowired
    ITranscriptSafetyDiffSetService iTranscriptSafetyDiffSetService;

    @Autowired
    ITranscriptSafetyScoreSetService iTranscriptSafetyScoreSetService;

    public List<StudentScore> getStudentScore(String openId) {

        TranscriptSet transcriptSet = transcriptSetMapper.selectById(1646797065822404608L);

        List<StudentScore> studentScoreList = new ArrayList<>();
        Quarter quarter = iQuarterService.getNowQuarter();

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        //studentQueryWrapper.inSql("id","select student_id from wechars_bind where openid = "+openId);
        studentQueryWrapper.inSql("id", "select student_id from student_grade where student_id in(select student_id from wechars_bind where openid ='" + openId + "') and status=1 and reading_status=1 and deleted = 0 and year_part=" + quarter.getYearPart() + " and quarter_num=" + quarter.getNum() + "");
        List<Student> studentList = iStudentService.list(studentQueryWrapper);
        for (Student student : studentList) {
            QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
            transcriptQueryWrapper.eq("student_id", student.getId());
            transcriptQueryWrapper.eq("year_part", quarter.getYearPart());//年份
            transcriptQueryWrapper.eq("quarter_num", quarter.getNum());//学期
            transcriptQueryWrapper.eq("exam_num", transcriptSet.getPatriarchExamNum());//考试名称

            List<Transcript> transcriptList = transcriptMapper.selectList(transcriptQueryWrapper);
            StudentScore studentScore = new StudentScore();
            studentScore.setId(student.getId());
            studentScore.setName(student.getName());
            studentScore.setTranscriptList(transcriptList);
            studentScore.setExamName(transcriptSet.getPatriarchExamName());
            studentScoreList.add(studentScore);
        }

        return studentScoreList;
    }


    @Override
    public List<EnteringStudent> getEnteringStudent(String[] gradeIdList) {

        TranscriptSet transcriptSet = transcriptSetMapper.selectById("1646797065822404608");
        Long schoolId = 0L;
        Long subjectsId = 0L;
        List<EnteringStudent> enteringStudentList = new ArrayList<>();
        for (String gradeId : gradeIdList) {
            schoolId = 0L;
            subjectsId = 0L;
            Grade grade = gradeMapper.selectById(gradeId);
            if (grade != null) {
                schoolId = grade.getSchoolId();
                Course course = courseMapper.selectById(grade.getCourseId());
                if (course != null) {
                    subjectsId = course.getSubjectsId();
                }
                EnteringStudent enteringStudent = new EnteringStudent();
                enteringStudent.setGradeId(grade.getId());
                enteringStudent.setGradeName(grade.getName());
                QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
                transcriptQueryWrapper.eq("school_id", schoolId);
                transcriptQueryWrapper.eq("grade_id", grade.getId());
                transcriptQueryWrapper.eq("subjects_id", subjectsId);
                transcriptQueryWrapper.eq("year_part", transcriptSet.getTeacherYearPart());
                transcriptQueryWrapper.eq("quarter_num", transcriptSet.getTeacherQuarterNum());
                transcriptQueryWrapper.eq("exam_num", transcriptSet.getTeacherExamNum());
                transcriptQueryWrapper.eq("is_statistics", 0);//是否已经录入分数，0否，1是
                List<Transcript> transcriptList = transcriptMapper.selectList(transcriptQueryWrapper);
                //System.out.println("transcriptListCount:"+transcriptList.size());
                enteringStudent.setTranscriptList(transcriptList);
                enteringStudentList.add(enteringStudent);
            }
        }
        return enteringStudentList;
    }


    /**
     * 成绩数据 （教务数据、成绩数据）
     */
    public List<ScoreData> scoreData(String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String yearClassId, String type, String include, String courseTypeId, TranscriptSafetyScoreSet tsSet11, TranscriptSafetyScoreSet tsSet12, TranscriptSafetyScoreSet tsSet21, TranscriptSafetyScoreSet tsSet22, TranscriptSafetyScoreSet tsSet32, List<TranscriptSafetyDiffSet> tsSetList) {

        List<ScoreData> scoreDataList = transcriptMapper.scoreData(Integer.valueOf(yearPart), Integer.valueOf(quarterNum), Integer.valueOf(examNum), Integer.valueOf(include), courseTypeId, yearClassId, subjectsId, schoolId, type, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, tsSetList);

        for (ScoreData scoreData : scoreDataList) {
            if (scoreData.getStudentAllCount() > 0) {
                scoreData.setNotCounted(scoreData.getStudentAllCount() - scoreData.getStuRealityCount());
                if (scoreData.getStuRealityCount() - scoreData.getNotCalcCount() > 0) {
                    scoreData.setNoPassRate(new BigDecimal(scoreData.getNoPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//不及格率
                    scoreData.setPassRate(new BigDecimal(scoreData.getPassCount() - scoreData.getNotCalcPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//优秀率
                    scoreData.setCarryRate(new BigDecimal(scoreData.getCarryCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//提分率
                    scoreData.setRetrogressRate(new BigDecimal(scoreData.getRetrogressCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));// 退步率
                } else {
                    scoreData.setNoPassRate(new BigDecimal(0));
                    scoreData.setPassRate(new BigDecimal(0));
                    scoreData.setCarryRate(new BigDecimal(0));
                    scoreData.setRetrogressRate(new BigDecimal(0));
                    scoreData.setRetrogressCount(0);
                }
                scoreData.setSecureRate(scoreData.getCarryRate().add(scoreData.getPassRate()));//安全值
            } else {
                scoreData.setNoPassRate(new BigDecimal(0));
                scoreData.setPassRate(new BigDecimal(0));
                scoreData.setCarryRate(new BigDecimal(0));
                scoreData.setSecureRate(new BigDecimal(0));
                scoreData.setRetrogressRate(new BigDecimal(0));
                scoreData.setRetrogressCount(0);
            }
        }

        return scoreDataList;
    }

    public IPage<ScoreDataDetail> scoreDataDetail(IPage<?> page, String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String teacherId, String gradeId, String isStatistics, String include, String studentName, String courseTypeId, TranscriptSafetyScoreSet tsSet11, TranscriptSafetyScoreSet tsSet12, TranscriptSafetyScoreSet tsSet21, TranscriptSafetyScoreSet tsSet22, TranscriptSafetyScoreSet tsSet32, List<TranscriptSafetyDiffSet> tsSetList, Integer column, Integer order) {
        Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;
        Integer upExamNum = 3;//期末
        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            //transcriptQueryWrapper.groupBy("teacher_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("2")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
        } else if (type.equals("3")) {
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.inSql("teacher_id","select admin_id from role_admin where role_id = 1568160887369994242 ");
        } else if (type.equals("4")) {
            //transcriptQueryWrapper.groupBy("school_id");
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        if (StringUtils.isBlank(yearClassId)) {
            if (gradeSection.equals("3")) {
                transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
            } else {
                transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
            }
        }

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);
        if (StringUtils.isNotBlank(isStatistics)) {
            if (isStatistics.equals("1")) {
                transcriptQueryWrapper.and(i -> i.eq("is_statistics", 1).eq("is_statistics_b", 1).or(j -> j.in("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)).or(j -> j.eq("year_class_id", 8).eq("quarter_num", 4).eq("subjects_id", 3).eq("is_statistics", 1)));
            } else {
                if (quarterNum.equals("4")) {
                    transcriptQueryWrapper.and(i -> i.eq("is_statistics", 0).or(j -> j.ne("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics_b", 0)).or(j -> j.ne("year_class_id", 8).eq("subjects_id", 3).eq("quarter_num", 4).eq("is_statistics_b", 0)));
                } else {
                    transcriptQueryWrapper.and(i -> i.eq("is_statistics", 0).or(j -> j.eq("is_statistics_b", 0)));
                }
            }
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        if (column != null && order != null) {
            if (column == 1) {
                // 根据本次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("score_start");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("score_start");
                }
            } else if (column == 2) {
                // 根据上次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("score_start_b");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("score_start_b");
                }
            }

        }

        IPage<ScoreDataDetail> iPage = transcriptMapper.scoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), upYearPart, upQuarterNum, upExamNum, Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, tsSetList);
        return iPage;
    }

    @Override
    public List<ScoreData> scoreTeamData(String subjectsId, String yearPart, String quarterNum, String examNum, String yearClassId, String include, String courseTypeId, String classify, String trackType, String teamId, TranscriptSafetyScoreSet tsSet11, TranscriptSafetyScoreSet tsSet12, TranscriptSafetyScoreSet tsSet21, TranscriptSafetyScoreSet tsSet22, TranscriptSafetyScoreSet tsSet32, List<TranscriptSafetyDiffSet> tsSetList) {
        Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;
        Integer upExamNum = 3;//期末
        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }

        List<ScoreData> scoreDataList = transcriptMapper.scoreTeamData(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), upYearPart, upQuarterNum, upExamNum, Integer.valueOf(include), courseTypeId, yearClassId, subjectsId, classify, trackType, teamId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, tsSetList);
        for (ScoreData scoreData : scoreDataList) {
            if (scoreData.getStudentAllCount() > 0) {
                scoreData.setNotCounted(scoreData.getStudentAllCount() - scoreData.getStuRealityCount());
            }
        }

        return scoreDataList;
    }

    @Override
    public IPage<ScoreDataDetail> scoreDataTeamDetail(IPage<?> page, String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String teacherId, String gradeId, String isStatistics, String include, String studentName, String courseTypeId, String classify, String trackType, String teamId, TranscriptSafetyScoreSet tsSet11, TranscriptSafetyScoreSet tsSet12, TranscriptSafetyScoreSet tsSet21, TranscriptSafetyScoreSet tsSet22, TranscriptSafetyScoreSet tsSet32, List<TranscriptSafetyDiffSet> tsSetList, Integer column, Integer order) {
        Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;
        Integer upExamNum = 3;//期末
        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "t.teacher_id", teacherId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "t.school_id", schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "t.grade_section", gradeSection);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "t.subjects_id", subjectsId);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "t.year_class_id", yearClassId);
        if (StringUtils.isBlank(yearClassId)) {
            if (gradeSection.equals("3")) {
                transcriptQueryWrapper.in("t.year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
            } else {
                transcriptQueryWrapper.in("t.year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
            }
        }


        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "t.grade_id", gradeId);
        if (StringUtils.isNotBlank(isStatistics)) {
            if (isStatistics.equals("1")) {
                transcriptQueryWrapper.and(i -> i.eq("t.is_statistics", 1).eq("t.is_statistics_b", 1).or(j -> j.in("t.year_class_id", 3).eq("t.quarter_num", 4).eq("t.is_statistics", 1)).or(j -> j.eq("t.year_class_id", 8).eq("t.quarter_num", 4).eq("t.subjects_id", 3).eq("t.is_statistics", 1)));
            } else {
                if (quarterNum.equals("4")) {
                    transcriptQueryWrapper.and(i -> i.eq("t.is_statistics", 0).or(j -> j.ne("t.year_class_id", 3).eq("t.quarter_num", 4).eq("t.is_statistics_b", 0)).or(j -> j.ne("t.year_class_id", 8).eq("t.subjects_id", 3).eq("t.quarter_num", 4).eq("t.is_statistics_b", 0)));
                } else {
                    transcriptQueryWrapper.and(i -> i.eq("t.is_statistics", 0).or(j -> j.eq("t.is_statistics_b", 0)));
                }
            }
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "t.grade_id", gradeId);

        if (column != null && order != null) {
            if (column == 1) {
                // 根据本次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("t.score_start");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("t.score_start");
                }
            } else if (column == 2) {
                // 根据上次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("t.score_start_b");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("t.score_start_b");
                }
            }
        }

        IPage<ScoreDataDetail> iPage = transcriptMapper.scoreDataTeamDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), upYearPart, upQuarterNum, upExamNum, Integer.valueOf(include), studentName, courseTypeId, yearClassId, subjectsId, gradeId, teacherId, classify, trackType, teamId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, tsSetList);

        return iPage;
    }

    public IPage<ScoreDataDetail> otherScoreDataDetail(IPage<?> page, String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String teacherId, String gradeId, String scoreDetailType, String status, String include, String studentName, String courseTypeId, String classify, String trackType, String teamId, Integer column, Integer order) {

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("2")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("3")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("4")) {

        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        if (gradeSection.equals("3")) {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
        } else {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
        }

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);
        //已统计的
        transcriptQueryWrapper.and(i -> i.eq("is_statistics", 1).eq("is_statistics_b", 1).or(j -> j.in("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)).or(j -> j.in("year_class_id", 8).eq("subjects_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)));
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
        transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());

        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
        safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }

        IPage<ScoreDataDetail> iPage = new Page<>();
        if (transcriptSafetyDiffSetList.size() == 0) {
            return iPage;
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        if (column != null && order != null) {
            if (column == 1) {
                // 根据本次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("score_start");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("score_start");
                }
            } else if (column == 2) {
                // 根据上次考试成绩排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("score_start_b");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("score_start_b");
                }
            } else if (column == 3) {
                // 根据退步分数排序
                if (order == 1) {
                    // 升序
                    transcriptQueryWrapper.orderByAsc("diff_score");
                } else {
                    // 降序
                    transcriptQueryWrapper.orderByDesc("diff_score");
                }
            }
        }

        //1:不及格人数详情，2：优秀人数详情，3提分人数详情
        if (scoreDetailType.equals("1")) {
            //小学：＜60分，初中：＜72分
            if (StringUtils.isBlank(status)) {
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            } else {
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }
        } else if (scoreDetailType.equals("2")) {
            //小学：≥90分，初中：≥96分
            if (StringUtils.isBlank(status)) {
                //transcriptQueryWrapper.and(i->i.eq("grade_section",1).ge("score_end",90).or(j->j.eq("grade_section",2).ge("score_end",96)));
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            } else {
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }

            if (type.equals("5")) {
                iPage = transcriptMapper.excellentSoreDataTeamDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, "1", classify, trackType, teamId);
            } else {
                iPage = transcriptMapper.excellentSoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, "1");
            }

        } else if (scoreDetailType.equals("3")) {
            //小学：低于90分，但有提分的学生，初中：低于96分，但有提分的学生
            //退步
            if (StringUtils.isNotBlank(status) && status.equals("0")) { // status 1为提分 0为退步
                transcriptQueryWrapper.lt("diff_score", 0);
                tsSet32.setPassScore(tsSet32.getRetrogressScore());
            } else {
                //提分
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }
            if (type.equals("5")) {
                iPage = transcriptMapper.carrySoreDataTeamDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, classify, trackType, teamId);
            } else {
                iPage = transcriptMapper.carrySoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList);
            }
        }

        return iPage;
    }
}
